using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

using CNCSoft.EData;
using Common;

namespace CNCSoft.ErpApps.TMS {

	/// <summary>
	/// Summary description for TienMatPhieuThuData.
	/// </summary>
	public class TienMatPhieuThuData: TMSDataBase  {

		public enum TL  {
			TienMatPT,
			TienMatPT_CT,
			DoiTac,
			LyDoThu,
			LyDoThu_Detail
		}

		private string sqlslPTTM = 
			"SELECT	[SoChungTu], [MaLoaiChungtu], [NgayChungtu], [MaDonVi], [MaDoitac], " +
			"		[Hoten], [DienGiai], [SoChungtuKemTheo], [Diachi], " +
			"		[TongtienUSD], [Tigia], [TongTien], [PhanTramDiscount], [TongtienDiscount], [ThucThu], [UpdateUserId], [UpdateDate], [LyDoThuChi] " +
			"FROM 	[TIENMAT] ";

		private string sqlslPTTM_CT = 
			"SELECT [ID], SoChungtu, TaiKhoanNo, MaTieuKhoanNo1, " +
			"		MaTieuKhoanNo2, TaiKhoanCo, MaTieuKhoanCo1, MaTieuKhoanCo2, " +
			"		[DienGiaiChiTiet], Sotien " +
			"FROM   TIENMAT_CHITIET TMCT ";

		public TienMatPhieuThuData() {

			//Chuan bi parameter de sinh ra so chung tu theo Prefix dinh truoc
			vg.TableName = "TIENMAT";
			vg.FieldName = "SoChungTu";
			vg.Prefix = WSInfo["TienMat.PhieuThu.MaLoaiCT"];

			ds.Tables.Add("TienMatPT");
			ds.Tables.Add("TienMatPT_CT");
			ds.Tables.Add("DoiTac");
			ds.Tables.Add("LyDoThu");
			ds.Tables.Add("LyDoThu_Detail");
            
			DanhMuc.fill(getTable(TL.DoiTac), DanhMuc.TLDM.DoiTac);
			DanhMuc.fill(getTable(TL.LyDoThu), DanhMuc.TLDM.LyDoThu);
			DanhMuc.fill(getTable(TL.LyDoThu_Detail), DanhMuc.TLDM.LyDoThu);

			initData("('')", true);
		}

		public void initData(string soChungTus, bool firstLoading) {
			if (!firstLoading) {
				getTable(TL.TienMatPT_CT).Clear();
				getTable(TL.TienMatPT).Clear();
			}

			SQLHelper.Fill(getTable(TL.TienMatPT), GlobalVariable.WSInfo.ConnectionString, 
				sqlslPTTM +"WHERE [SoChungTu] In " +soChungTus);

			SQLHelper.Fill(getTable(TL.TienMatPT_CT), GlobalVariable.WSInfo.ConnectionString, 
				sqlslPTTM_CT +"WHERE TMCT.SoChungTu In " +soChungTus);

			if (firstLoading) {
				// Set primary key(s)
				getTable(TL.TienMatPT).PrimaryKey = new DataColumn[]{getTable(TL.TienMatPT).Columns["SoChungTu"]};
				getTable(TL.TienMatPT_CT).PrimaryKey = new DataColumn[]{getTable(TL.TienMatPT_CT).Columns["ID"]};

				// Set relations
				ds.Relations.Add("M_D", getTable(TL.TienMatPT).Columns["SoChungTu"],
					getTable(TL.TienMatPT_CT).Columns["SoChungTu"]);

				// Set default values
				getTable(TL.TienMatPT).Columns["SoChungTu"].DefaultValue = "";
				getTable(TL.TienMatPT).Columns["NgayChungTu"].DefaultValue = GlobalVariable.WSInfo.WorkingDate;
                getTable(TL.TienMatPT).Columns["MaDonVi"].DefaultValue = GlobalVariable.WSInfo.CompanyID;
				getTable(TL.TienMatPT).Columns["UpdateUserId"].DefaultValue = GlobalVariable.WSInfo.UserID;
				getTable(TL.TienMatPT).Columns["UpdateDate"].DefaultValue = GlobalVariable.WSInfo.ServerDate;
                getTable(TL.TienMatPT).Columns["TongTienUSD"].DefaultValue = 0;
                getTable(TL.TienMatPT).Columns["Tigia"].DefaultValue = 1;
				getTable(TL.TienMatPT).Columns["TongTien"].DefaultValue = 0;
                getTable(TL.TienMatPT).Columns["PhanTramDiscount"].DefaultValue = 0;
                getTable(TL.TienMatPT).Columns["TongTienDiscount"].DefaultValue = 0;
                getTable(TL.TienMatPT).Columns["ThucThu"].DefaultValue = 0;
				getTable(TL.TienMatPT).Columns["MaLoaiChungTu"].DefaultValue =GlobalVariable.WSInfo["TienMatPhieuThu.MaLoaiChungTu", "PT"];;

				getTable(TL.TienMatPT_CT).Columns["ID"].AutoIncrement = true;
				getTable(TL.TienMatPT_CT).Columns["ID"].AutoIncrementStep = 1;
				getTable(TL.TienMatPT_CT).Columns["SoTien"].DefaultValue = 0;
				getTable(TL.TienMatPT_CT).Columns["TaiKhoanNo"].DefaultValue = GlobalVariable.WSInfo["TienMat.PhieuThu.MaTKN", "1111"];
                getTable(TL.TienMatPT_CT).Columns["DienGiaiChiTiet"].DefaultValue = "";
			}
		}

		public bool createNewRow() {
			try {
				DataRow dr = getTable(TL.TienMatPT).NewRow();
                vg.DateForGenerating = GlobalVariable.WSInfo.WorkingDate;   //Generate theo ngay dang nhap
                vg.Prefix = "PT";
                vg.VoucherNoFormatDesign = VoucherNoGenerator.VoucherNoFormat.Prefix_YY_MM_Delimiter_SerialNo;
				dr["SoChungTu"] = vg.generateVoucherNo();
				getTable(TL.TienMatPT).Rows.Add(dr);

				return true;
			}
			catch {
				MessageBox.Show(RS.ERROR_CREATE_NEW_ROW, RS.ERROR_CREATE_NEW_ROW_CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
			return false;
		}

		public bool update() {
			try {
				new SimpleMasterDetailUpdater(sqlslPTTM, sqlslPTTM_CT, getTable(TL.TienMatPT), getTable(TL.TienMatPT_CT), GlobalVariable.WSInfo.ConnectionString);
				ds.AcceptChanges();
				return true;
			} catch (Exception ex) {
				MessageBox.Show(RS.ERROR_UPDATE +"\n" +ex.Message, RS.ERROR_UPDATE_CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
			return false;
		}

		public DataTable getTable(TL index) {
			return ds.Tables[(int)index];
		}
	}
}
